#include "user_include.h"

static void drv_init(void);
static int8_t drv_write(uint8_t *buffer, uint8_t size);
static int8_t drv_read(uint8_t *buffer, uint8_t size);
static void drv_task(void);
#define I2C_ADDR 0X40 //
struct driver_pcf8574_def driver_pcf8574 = {
    .init = drv_init,
    .write = drv_write,
    .read = drv_read,
    .task = drv_task,
};
static osSemaphoreId_t sid_Semaphore_tx;
static osSemaphoreId_t sid_Semaphore_rx;

static void drv_init(void)
{
}

static int8_t drv_write(uint8_t *buffer, uint8_t size)
{
    HAL_StatusTypeDef hal_status;

#if 0
    hal_status = HAL_I2C_Master_Transmit_DMA(&hi2c2, I2C_ADDR, buffer, size);
    if (hal_status != HAL_OK)
    {
        return 1;
    }

    os_status = osSemaphoreAcquire(sid_Semaphore_tx, 100);
    if (os_status != HAL_OK)
    {
        return 1;
    }
#else
    osSemaphoreAcquire(sem_i2c2Handle, 100);
    hal_status = HAL_I2C_Master_Transmit(&hi2c2, I2C_ADDR, buffer, size, 10);
    osSemaphoreRelease(sem_i2c2Handle);
    return 0;
#endif
}

static int8_t drv_read(uint8_t *buffer, uint8_t size)
{
    HAL_StatusTypeDef hal_status;
    osSemaphoreAcquire(sem_i2c2Handle, 100);
    hal_status = HAL_I2C_Master_Receive(&hi2c2, I2C_ADDR, buffer, size, 10);
    osSemaphoreRelease(sem_i2c2Handle);
    return 0;
}
static void drv_task(void)
{
    uint8_t buffer[10];
    driver_pcf8574.init();
    while (1)
    {
        buffer[0] = 0xff;
        // driver_pcf8574.write(buffer, 1);
        osDelay(1000);
        buffer[0] = 0x00;
        // driver_pcf8574.write(buffer, 1);
        osDelay(1000);
    }
}